package com.test.daily.leetcode.y2022.m04.day0430.v2;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

/**
 * @author Tom on 2022/4/30
 */
public class Solution {
    public static void main(String[] args) {

    }
    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
        PriorityQueue<Integer> pq = new PriorityQueue(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
        if(root1 != null){
            addToPq(root1, pq);
        }
        if(root2 != null){
            addToPq(root2, pq);
        }
        List<Integer> list = new ArrayList<>();
        while(pq.size() != 0){
            list.add(pq.poll());
        }
        return list;
    }
    public void addToPq(TreeNode root, PriorityQueue<Integer> pq){
        if(root.left != null){
            addToPq(root.left, pq);
        }
        if(root.right != null){
            addToPq(root.right, pq);
        }
        if(root != null){
            pq.add(root.val);
        }
    }


    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

}
